/**
 * @class Bible.plugins.TabPanelAddButton
 * @extends Object
 */
Bible.plugins.TabPanelAddButton = Ext.extend(Object, {
    /**
     * @cfg {Number} maxLimit
     */
    maxLimit: 10,
    /**
     * @cfg {Ext.Button} addButton
     */

    constructor: function(cfg) {
        console.debug(cfg);
        Ext.apply(this, cfg || {});
        //Bible.plugins.TabPanelAddButton.superclass.constructor.call(this, cfg);
    },

    init: function(tabPanel){
        var me = this;
        me.tabPanel = tabPanel;

        tabPanel.afterMethod('onRemove', me.onRemove, me);
        tabPanel.afterMethod('onAdd', me.onAdd, me);
        tabPanel.afterMethod('delegateUpdates', me.delegateUpdates, me);
    },

    // private
    createAddButton : function(){
        var me = this,
            tabPanel = me.tabPanel,
            h = tabPanel.stripWrap.dom.offsetHeight,
            sr = tabPanel.pos.insertFirst({
                cls:'x-tab-left-btn-new-tab x-toolbar'
            });
        tabPanel.pos.addClass('x-tab-scrolling');

        console.debug('createAddButton');
        me.addButton.render(sr);
        sr.setHeight(h);
        me.btnAddTabContainer = sr;
    },

//    onAdd: Ext.TabPanel.prototype.onAdd.createSequence(function() {
    onAdd: function() {
        console.debug('onAdd');
        if(this.tabPanel.items.getCount() > this.maxLimit-1){
            this.addButton.setDisabled(true);
        }
    },
//    onRemove: Ext.TabPanel.prototype.onRemove.createSequence(function() {
    onRemove: function() {
        if(this.tabPanel.items.getCount() < this.maxLimit){
            this.addButton.setDisabled(false);
        }
    },
//    delegateUpdates: Ext.TabPanel.prototype.delegateUpdates.createSequence(function() {
    delegateUpdates: function() {
        var me = this,
            tabPanel = me.tabPanel,
            btnAddTabContainer = me.btnAddTabContainer;
        if(tabPanel.pos){
            if(!btnAddTabContainer){
                me.createAddButton();
            } else {
                if(tabPanel.scrolling){
                    tabPanel.stripWrap.setStyle('marginLeft', 53 + 'px');
                    btnAddTabContainer.setLeft(18);
                } else {
                    tabPanel.stripWrap.setStyle('marginLeft', 35 + 'px');
                    btnAddTabContainer.setLeft(0);
                    tabPanel.pos.addClass('x-tab-scrolling');
                }
            }
        }
        // TODO when add new tab scroll, (wrap width - btnAddTab.width)
    }
});